import {HttpProvider, AjaxOption} from '../../common/http'
import {Injectable} from '@angular/core';
import {Helper} from "../../common/Helper";
import {API} from "../../common/api";

@Injectable()
export class LoginProvider {

  constructor(public http: HttpProvider,
              public helper: Helper) {
    console.log('Hello LoginProvider Provider');
  }

  login(options: AjaxOption){
    this.helper.loading('正在登录..')
    return this.http.requestWithoutToken({
      type: 'POST',
      url: API.USER_LOGIN,
      data: options.data ? options.data : {}
    }).then(data => {
      this.helper.stopLoading()

      this.helper.setStorage('user/phone',options.data.cellphone)
      this.helper.setStorage('user/login','1')
      this.helper.setStorage('token/accessToken',data.accessToken)
      this.helper.setStorage('token/refreshToken',data.refreshToken)
      this.helper.setStorage('token/type',data.type)

      options.complete && options.complete()
      options.success && options.success(data)
    }).catch(err => {
      this.helper.stopLoading()
      this.http.toastMsg(err.msg)
      options.complete && options.complete()
      options.fail && options.fail(err)
    })
  }

}
